package com.sk.skplanet.commute.views;

import java.util.List;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sk.skplanet.commute.R;
import com.sk.skplanet.commute.actions.SortingHandler;
import com.sk.skplanet.commute.controllers.DialogController;
import com.sk.skplanet.commute.controllers.StopInfoController;
import com.sk.skplanet.commute.daofactory.LocalDaoFactory;
import com.sk.skplanet.commute.fragments.AddStopTabbed;
import com.sk.skplanet.commute.vo.Data;
import com.sk.skplanet.commute.vo.Point;
import com.sk.skplanet.commute.vo.StopInfo;

public class PointList extends Activity {
	LinearLayout layout_pointlist;
	int pointId;
	Point point;
	Thread api_connectingThread;
	SortingHandler sortingHandler;
	boolean api_connectingThread_run;
	
	final int SEARCH_BUS_ACTIVITY = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pointlist);

		layout_pointlist = (LinearLayout) findViewById(R.id.layout_pointlist);
		// layout_pointlist_title.setBackgroundColor(Color.parseColor(Data.layout_pointlist_titleColor));
		layout_pointlist.setBackgroundColor(Color.parseColor(Data.layout_pointColor));

		// Preparing presentation data
		pointId = getIntent().getIntExtra("pointId", -1);
		point = LocalDaoFactory.getPoint(pointId);
		List<StopInfo> stopInfos = LocalDaoFactory.getStopinfosByPointID(pointId);
		
		// Log.i("포인트리스트 접근", route.toString());
		if (pointId > 0) {
//			textview_pointlist_title.setText(point.getName());
			// textview_pointlist_title.setTextColor(Color.parseColor(Data.textview_pointColor));
//			textview_pointlist_title.setTextColor(Color.parseColor("#FEFEFE"));
			
			
			for(StopInfo s : stopInfos) {
				layout_pointlist.addView(StopInfoController.makeInfo(this, s));
			}
//			if (route.getPoints() != null) {
//				for (int pointPosition = 0; pointPosition < route.getPoints().size(); pointPosition++)
//					layout_pointlist.addView(PointController.makePoint(this, pointId, pointPosition));
//			}
		}

//		button_pointlist_addlpoint = (Button) findViewById(R.id.button_pointlist_addlpoint);
//
//		button_pointlist_addlpoint.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
////				DialogController.createPointDialog(PointList.this, pointId).show();
//				DialogController.createStationDialogAtLocal(PointList.this, pointId).show();
//			}
//		});

		sortingHandler = new SortingHandler();

//		api_connectingThread_run = true;
//		api_connectingThread = new API_cennectingThread(PointList.this, layout_pointlist, route,
//				api_connectingThread_run, sortingHandler);
//
//		api_connectingThread.start();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.pointlist, menu);
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setTitle(point.getName());
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent;
		
		switch(item.getItemId()) {
			case android.R.id.home:
				intent = new Intent(this, RouteList.class);
	            //intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
	            startActivity(intent);
	            finish();
				break;
			case R.id.action_add_points:
				DialogController.createStationDialogAtLocal(PointList.this, pointId).show();
				break;
			case R.id.action_add_points_use_activity:
				intent = new Intent(this, AddStopTabbed.class);
				startActivityForResult(intent, SEARCH_BUS_ACTIVITY);
//				intent = new Intent(this, AddStopActivity.class);
//				startActivity(intent);		// 추후 ForResult로 대체
				//finish();
				break;
			default:
				return false;
		}
		
		return true;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch(requestCode) {
			case SEARCH_BUS_ACTIVITY:
				if(resultCode == RESULT_OK) {
					data.getExtras().getInt("code");
				}
				break;
			default:
				break;
		}
		
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
//		api_connectingThread.interrupt();
//		api_connectingThread_run = false;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {

			api_connectingThread_run = false;
			Intent intentRouteList = new Intent(PointList.this, RouteList.class);
			startActivity(intentRouteList);
			finish();
		}
		return false;
		// return super.onKeyDown(keyCode, event);
	}

}
